package com.guide.mvc;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.guide.bizdto.AccountSel;
import com.guide.bizdto.CommentsParam;
import com.guide.bizdto.IndexSel;
import com.guide.bizdto.TravelTeamParam;
import com.guide.bizdto.TravelTeamSel;
import com.guide.bizdto.UserInfoParam;
import com.guide.dao.PersonalDao;
import com.guide.domain.Account;
import com.guide.domain.Guide;
import com.guide.domain.Personal;
import com.guide.domain.Travelorg;
import com.guide.dto.RetDTO;
import com.guide.enums.ErrorCodeEnum;
import com.guide.exception.ServiceException;
import com.guide.service.AccountService;
import com.guide.service.CommentsService;
import com.guide.service.GuideService;
import com.guide.service.TravelTeamService;
import com.guide.utils.ConstantUtils;
import com.guide.utils.QiNiuUtils;
import com.guide.utils.SmsUtils;

@Controller
@RequestMapping(value = { "/core","/open","/"}, produces = "application/json")
public class GuideController extends AbstractController{

	@Autowired
	private AccountService accountService;
	@Autowired
	private TravelTeamService travelTeamService;
	@Autowired
	private GuideService guideService;
/*	@Autowired
	private CommentsService commentsService;*/
	
	
	
	@RequestMapping("/login")
	public RetDTO login(@RequestBody UserInfoParam userInfoParam) {
		RetDTO retDTO = new RetDTO();
		Account account  =accountService.login(userInfoParam);
		//this.httpSession.removeAttribute(LOGIN_USER);
		loginAfter(account);
		//Personal personal = 
		retDTO.addResult("userType", account.getUserType());
		retDTO.markSuccess();
		//SmsUtils.sendSmsByTP("13337885351", "尊敬的用户，您刚刚登录了约导游账户，如非本人操作，请忽略此信息。退订回复TD【约导游】");
		return retDTO;
	}
	private void loginAfter(Account account){
		this.httpSession.setAttribute(LOGIN_USER, account);
		if(account.getUserType()!= ConstantUtils.ACCOUNT_TYPE_ADMIN) {
			this.httpSession.setAttribute(LOGIN_DETAL,accountService.getAccountDetailByAccount(account));
		}
		
		refreshUnreadCount();
		if(accountService.checkAccountPrefect(account)){
			this.httpSession.setAttribute(SHOW_WANSHAN, 1);
		}else{
			this.httpSession.setAttribute(SHOW_WANSHAN, 0);
		}
	}
	@RequestMapping("/refreshWanShan")
	public RetDTO refreshWanShan() {
		RetDTO retDTO = new RetDTO();
		this.httpSession.removeAttribute(SHOW_WANSHAN);
		this.httpSession.setAttribute(SHOW_WANSHAN, 2);
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	@RequestMapping("/logout")
	public RetDTO logout() {
		RetDTO retDTO = new RetDTO();
		this.httpSession.removeAttribute(LOGIN_USER);
		this.httpSession.removeAttribute(LOGIN_DETAL);
		this.httpSession.removeAttribute(SHOW_WANSHAN);
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	
   @RequestMapping({"/getUpToken"})
    public RetDTO getUpToken() {
	    Map retMap = new HashMap();
	    RetDTO retDTO = new RetDTO();
	    retDTO.addResult("uptoken", QiNiuUtils.getUpToken());
	    retDTO.markSuccess();
	    return retDTO;
    }
	@RequestMapping("/register")
	public RetDTO register(@RequestBody UserInfoParam userInfoParam){
		RetDTO retDTO = new RetDTO();
		Account account = accountService.register(userInfoParam);
		loginAfter(account);
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/modifyUserDetail")
	public RetDTO modifyUserDetail(@RequestBody UserInfoParam userInfoParam){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		accountService.modifyAccountDetail(account, userInfoParam);
		this.httpSession.removeAttribute(LOGIN_DETAL);
		this.httpSession.setAttribute(LOGIN_DETAL,accountService.getAccountDetailByAccount(account));
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	@RequestMapping("/adminModifyPass")
	public RetDTO adminModifyPass(@RequestBody AccountSel accountSel){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		accountService.adminModifyPass(accountSel, account);
		this.httpSession.removeAttribute(LOGIN_DETAL);
		this.httpSession.setAttribute(LOGIN_DETAL,accountService.getAccountDetailByAccount(account));
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getAccountDetail")
	public RetDTO getAccountDetail(){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		//accountService.register(userInfoParam);
		retDTO.addResult("userDetail", accountService.getAccountDetailByAccount(account));
		retDTO.markSuccess();
		return retDTO;
	}
	
	
	@RequestMapping("/getAccountDetailByTeamId")
	public RetDTO getAccountDetail(@RequestParam Long teamId){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		retDTO.addResult("userDetail", accountService.getAccountDetailByTeamId(teamId));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getAccountList")
	public RetDTO getAccountList(@RequestBody AccountSel sel){
		RetDTO retDTO = new RetDTO();
		//accountService.register(userInfoParam);
		Map<String,Object> retMap = accountService.getAccountList(sel);
		retDTO.addResult(ConstantUtils.DATE_LIST, retMap.get(ConstantUtils.DATE_LIST));
		retDTO.addResult(ConstantUtils.TOTAL_COUNT, retMap.get(ConstantUtils.TOTAL_COUNT));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/auditAccount")
	public RetDTO auditAccount(@RequestBody AccountSel sel){
		RetDTO retDTO = new RetDTO();
		Account account = accountService.getAccountById(Long.valueOf(sel.getUserId()));
		if(account!=null){
			account.setStatus(ConstantUtils.REGISTER_APPLY_1);
			account.setMtime(new Date());
			accountService.modifyAccount(account);
		}else{
			throw new ServiceException(ErrorCodeEnum.SYSTEM_ERROR);
		}
		retDTO.markSuccess();
		return retDTO;
	}
	
	
	
	@RequestMapping("/createTravelTeam")
	public RetDTO createTravelTeam(@RequestBody TravelTeamParam travelTeamParam){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		if(account.getUserType()==ConstantUtils.ACCOUNT_TYPE_0) {//lxs
			Travelorg  travelorg = accountService.getTravelorgByUserId(account.getUserId());
			travelTeamService.createTravelTeam(travelTeamParam, account, travelorg.getId());
		}else{//personal
			Personal personal = accountService.getPersonalByUserId(account.getUserId());
			travelTeamService.createTravelTeam(travelTeamParam, account, personal.getId());
		}
		//SMS旅行社发团，给相关导游发短信在service层
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/modifyTravelTeam")
	public RetDTO modifyTravelTeam(@RequestBody TravelTeamParam travelTeamParam){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		if(account.getUserType()==ConstantUtils.ACCOUNT_TYPE_0) {//lxs
			//Travelorg  travelorg = accountService.getTravelorgByUserId(account.getUserId());
			travelTeamService.updateTravelTeam(travelTeamParam, account);
		}else{//personal
			//Personal personal = accountService.getPersonalByUserId(account.getUserId());
			travelTeamService.updateTravelTeam(travelTeamParam, account);
		}
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	
	
	//团信息列表
	@RequestMapping("/getTravelTeamList")
	public RetDTO getTravelTeamList(@RequestBody TravelTeamSel travelTeamSel){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		Map<String,Object> retMap = travelTeamService.getTravelTeamList(account, travelTeamSel);
		retDTO.addResult(ConstantUtils.DATE_LIST, retMap.get(ConstantUtils.DATE_LIST));
		retDTO.addResult(ConstantUtils.TOTAL_COUNT, retMap.get(ConstantUtils.TOTAL_COUNT));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getNewTravelTeamList")
	public RetDTO getNewTravelTeamList(@RequestBody TravelTeamSel travelTeamSel){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		Map<String,Object> retMap = travelTeamService.getNewTravelTeamList(travelTeamSel);
		retDTO.addResult(ConstantUtils.DATE_LIST, retMap.get(ConstantUtils.DATE_LIST));
		retDTO.addResult(ConstantUtils.TOTAL_COUNT, retMap.get(ConstantUtils.TOTAL_COUNT));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getNewTravelTeamListByGuide")
	public RetDTO getNewTravelTeamListByGuide(@RequestBody TravelTeamSel travelTeamSel){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		Map<String,Object> retMap = travelTeamService.getNewTravelTeamListByGuide(travelTeamSel,account);
		retDTO.addResult(ConstantUtils.DATE_LIST, retMap.get(ConstantUtils.DATE_LIST));
		retDTO.addResult(ConstantUtils.TOTAL_COUNT, retMap.get(ConstantUtils.TOTAL_COUNT));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getTravelTeamById")
	public RetDTO getTravelTeamList(@RequestParam Long id){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		retDTO.addResult("travelTeam", travelTeamService.getTravelTeamById(id));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/applyTravelTeam")
	public RetDTO applyTravelTeam(@RequestParam Long teamId){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		//retDTO.addResult("travelTeam", travelTeamService.getTravelTeamById(id));
		guideService.applyTravelTeam(account, teamId);
		//SMS导游申请团，给发团人发短信
		 try{
            UserInfoParam userInfoParam = accountService.getAccountDetailByTeamId(Long.valueOf(teamId));
            Guide guide = guideService.getGuideByUserId(account.getUserId());
    		if(userInfoParam!=null && guide!=null){
    			SmsUtils.sendSmsByTP(userInfoParam.getPhone(), String.format(ConstantUtils.SMS_GUIDE_APPLY_TRAVELOR,userInfoParam.getName(), guide.getName()));
    		}
        }catch(Exception e){
        	logger.error("---sms----导游申请旅行社的行程发短信出错---"+e.getMessage());
        }
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/removeTravelTeam")
	public RetDTO removeTravelTeam(@RequestParam Long teamId){
		RetDTO retDTO = new RetDTO();
		//retDTO.addResult("travelTeam", travelTeamService.getTravelTeamById(id));
		travelTeamService.removeTravelTeam(teamId);
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/acceptApply")
	public RetDTO acceptApply(@RequestParam Long teamId,@RequestParam Long guideId){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		guideService.acceptApplyGuide(teamId, guideId);
		
		//SMS给当前导游发送被确认短信
		Guide guide = guideService.getGuideById(guideId);
		if(guide!=null){
			SmsUtils.sendSmsByTP(guide.getPhone(), String.format(ConstantUtils.SMS_TRAVELOR_APPLY_GUIDE,guide.getName(), String.format(ConstantUtils.SMS_TEAM_LINK, teamId)));
		}
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	/**
	 * 得到一个团的申请列表
	 * @param teamId
	 * @return
	 */
	@RequestMapping("/getApplyGuideListByTeamId")
	public RetDTO getApplyGuideListByTeamId(@RequestParam Long teamId){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		retDTO.addResult("guideList", guideService.getApplyGuidesByTeamId(teamId));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getTravelTeamByStatus")
	public RetDTO getTravelTeamByStatus(@RequestBody TravelTeamSel travelTeamSel){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		Map<String,Object> retMap = travelTeamService.getTravelTeamByStatus(account, travelTeamSel);
		retDTO.addResult(ConstantUtils.DATE_LIST, retMap.get(ConstantUtils.DATE_LIST));
		retDTO.addResult(ConstantUtils.TOTAL_COUNT, retMap.get(ConstantUtils.TOTAL_COUNT));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getTravelTeamByTeamId")
	public RetDTO getTravelTeamByTeamId(@RequestParam Long teamId){
		RetDTO retDTO = new RetDTO();
		//Account account = this.getCurrAccount();
		retDTO.addResult("travelTeam", travelTeamService.getTravelTeamById(teamId));
		retDTO.markSuccess();
		return retDTO;
	}
	
	/*
	 * 通过teamId得到某个团确认的导游信息
	 */
	@RequestMapping("/getGuideByConfirm")
	public RetDTO getGuideByConfirm(@RequestParam Long teamId){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		retDTO.addResult("guide", accountService.getApplyGuideByConfirm(teamId));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getTravelorgById")
	public RetDTO getTravelorgById(@RequestParam Long travelorgId){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		retDTO.addResult("travelorg", accountService.getTravelorgById(travelorgId));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getTravelorgNoLoginById")
	public RetDTO getTravelorgNoLoginById(@RequestParam Long id){
		RetDTO retDTO = new RetDTO();
		//Account account = this.getCurrAccount();
		retDTO.addResult("travelorg", accountService.getTravelorgById(id));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getGuideNoLoginById")
	public RetDTO getGuideNoLoginById(@RequestParam Long id){
		RetDTO retDTO = new RetDTO();
		//Account account = this.getCurrAccount();
		retDTO.addResult("guide", accountService.getGuideById(id));
		retDTO.markSuccess();
		return retDTO;
	}
	@RequestMapping("/getPersonalNoLoginById")
	public RetDTO getPersonalNoLoginById(@RequestParam Long id){
		RetDTO retDTO = new RetDTO();
		//Account account = this.getCurrAccount();
		retDTO.addResult("personal", accountService.getPersonalById(id));
		retDTO.markSuccess();
		return retDTO;
	}
	/**
	 * 导游评价旅行社
	 * @param commentsParam
	 * @return
	 */
	@RequestMapping("/commentTravelTeam")
	public RetDTO commentTravelTeam(@RequestBody CommentsParam commentsParam){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		commentsService.commentsTravelorg(commentsParam,account);
		//SMS导游评价旅行社
        try{
            UserInfoParam userInfoParam = accountService.getAccountDetailByTeamId(Long.valueOf(commentsParam.getTeamId()));
            Guide guide = guideService.getGuideById(Long.valueOf(commentsParam.getGuideId()));
    		if(userInfoParam!=null && guide!=null){
    			SmsUtils.sendSmsByTP(userInfoParam.getPhone(), String.format(ConstantUtils.SMS_GUIDE_COMMENT_TRAVELOR,userInfoParam.getName(), guide.getName()));
    		}
        }catch(Exception e){
        	logger.error("---sms----导游评价旅行社出错---"+e.getMessage());
        }
    
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	
	/**
	 * 旅行社评价导游
	 * @param commentsParam
	 * @return
	 */
	@RequestMapping("/commentGuide")
	public RetDTO commentGuide(@RequestBody CommentsParam commentsParam){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		commentsService.commentsGuide(commentsParam,account);
		//SMS旅行社评价导游
		Guide guide = guideService.getGuideById(Long.valueOf(commentsParam.getGuideId()));
		
		if(guide!=null){
			SmsUtils.sendSmsByTP(guide.getPhone(), String.format(ConstantUtils.SMS_TRAVELOR_COMMENT_GUIDE, guide.getName()));
		}
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getCommentTravelByTeamId")
	public RetDTO getCommentTravelByTeamId(@RequestParam Long teamId,@RequestParam Long guideId){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		//commentsService.commentsGuide(commentsParam,account);
		retDTO.addResult("comments", commentsService.getCommentsTravelByTeamId(teamId, guideId));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getCommentGuideByTeamId")
	public RetDTO getCommentGuideByTeamId(@RequestParam Long teamId,@RequestParam Long guideId){
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		//commentsService.commentsGuide(commentsParam,account);
		retDTO.addResult("comments", commentsService.getCommentsGuidelByTeamId(teamId, guideId));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getGuideListByScore")
	public RetDTO getGuideListByScore(@RequestBody IndexSel indexSel){
		RetDTO retDTO = new RetDTO();
		//Account account = this.getCurrAccount();
		//commentsService.commentsGuide(commentsParam,account);
		Map<String,Object> retMap = accountService.getGuideListByScore(indexSel);
		retDTO.addResult(ConstantUtils.DATE_LIST, retMap.get(ConstantUtils.DATE_LIST));
		retDTO.addResult(ConstantUtils.TOTAL_COUNT, retMap.get(ConstantUtils.TOTAL_COUNT));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getTravelorgListByScore")
	public RetDTO getTravelorgListByScore(@RequestBody IndexSel indexSel){
		RetDTO retDTO = new RetDTO();
		//Account account = this.getCurrAccount();
		Map<String,Object> retMap = accountService.getTravelorgListByScore(indexSel);
		retDTO.addResult(ConstantUtils.DATE_LIST, retMap.get(ConstantUtils.DATE_LIST));
		retDTO.addResult(ConstantUtils.TOTAL_COUNT, retMap.get(ConstantUtils.TOTAL_COUNT));
		retDTO.markSuccess();
		return retDTO;
	}
	
	
	

}
